<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use think\Model;

class Notice extends Model{
    protected $autoWriteTimestamp = false;
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='id desc',$limit=10,$filed='*'){
         $list = $this
            ->field($filed)
            ->with(['user'=>function($query){
                $query->field('id,username,nickname,sex,head,rong_token');
            },'article'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        return $list;
    }

    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList1($where=[],$order='id desc',$limit=10,$filed='*'){
        $list = $this
            ->field('update_time',true)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        return $list;
    }

    public function user(){
        return $this->hasOne('member','id','mid');
    }

    /**
     * 添加通知
     * @param int $mid 用户ID
     * @param string $title 主题
     * @param string $content 内容
     * @param int $type 类型
     * @param int $cid 跳转
     * @param int $rid 目标
     * @param int $link 链接
     * @param int $msg_id 系统消息ID
     * @param string $out
     * @return bool
     */
    public function addNew($mid=0,$title='',$content='',$type=0,$cid=0,$rid=0,$link='',$msg_id=0,&$out=''){
        $data = [
            'mid'=>$mid,
            'msg_id'=>$msg_id,
            'title'=>$title,
            'content'=>$content,
            'type'=>$type,
            'cid'=>$cid,
            'rid'=>$rid,
            'link'=>$link,
            'create_time'=>time()
        ];
        $valid = new \app\first\validate\Notice();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        if(!$this->save($data)){
            $out = '操作失败';
            return false;
        }
        return true;
    }

    /**
     * @param array $uid
     * @param string $title
     * @param string $content
     * @param int $type
     * @param int $cid
     * @param int $rid
     * @param string $link
     * @param int $msg_id
     * @param string $out
     * @return bool
     * @throws \Exception
     */
    public function addNewAll($uid=[],$title='',$content='',$type=0,$cid=0,$rid=0,$link='',$msg_id=0,&$out=''): bool
    {
        $data = [];
        $time = time();
        foreach ($uid as $v){
            array_push($data,[
                'mid'=>$v,
                'msg_id'=>$msg_id,
                'title'=>$title,
                'content'=>$content,
                'type'=>$type,
                'cid'=>$cid,
                'rid'=>$rid,
                'link'=>$link,
                'create_time'=>$time
            ]);
        }
        if(!$data){
            $out = '没有数据';
            return false;
        }
        if(!$this->saveAll($data)){
            $out = '操作失败';
            return false;
        }
        return true;
    }
}